package com.kk.single;

import java.util.HashSet;

/**
 * 线程安全的懒汉式
 * 低效率的
 *
 * @author 王志强
 * @date 2021-12-09
 */
public class Singleton2 {


    private static Singleton2 instance;

    private Singleton2() {
    }

    public static synchronized Singleton2 getInstance() {
        if (instance == null) {
            instance = new Singleton2();
        }
        return instance;
    }

    public static void main(String[] args) {
        HashSet<Object> hashSet = new HashSet<>();
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                Singleton2 instance = Singleton2.getInstance();
                hashSet.add(instance);

            }).start();
        }

        System.out.println(hashSet);
    }

}
